<template>
  <div class="ad-banner" :class="position">
    <div class="ad-content">
      <span class="ad-label">广告</span>
      <slot>
        <a :href="ad.link" target="_blank" rel="noopener noreferrer">
          <img :src="ad.image" :alt="ad.title" class="ad-image">
        </a>
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdBanner',
  props: {
    position: {
      type: String,
      default: 'normal',
      validator: value => ['normal', 'sidebar', 'banner'].includes(value)
    },
    ad: {
      type: Object,
      default: () => ({
        image: 'https://via.placeholder.com/300x100?text=广告位招租',
        link: '#',
        title: '广告位'
      })
    }
  }
}
</script>

<style scoped>
.ad-banner {
  margin: 15px 0;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.ad-banner.normal {
  background: #f5f7fa;
  padding: 10px;
}

.ad-banner.sidebar {
  margin-bottom: 20px;
}

.ad-banner.banner {
  margin: 20px 0;
}

.ad-content {
  position: relative;
}

.ad-label {
  position: absolute;
  right: 5px;
  top: 5px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  font-size: 12px;
  padding: 1px 4px;
  border-radius: 2px;
  z-index: 1;
}

.ad-image {
  display: block;
  width: 100%;
  border-radius: 4px;
}

.ad-banner.sidebar .ad-image {
  max-height: 150px;
  object-fit: cover;
}

.ad-banner.banner .ad-image {
  max-height: 100px;
  object-fit: contain;
}
</style>